package com.dingtalk.yuanhua_spring_project.service.impl;

import com.aliyun.tea.TeaException;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.yuanhua_spring_project.service.DingTalkDingPan;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class DingTalkDingPanimpl implements DingTalkDingPan {
    private static final Logger logger = LoggerFactory.getLogger(DingTalkDingPanimpl.class);
    @Override
    public String getAccessToken() {
        return new DingTalkAccessimpl().getAccessToken();
    }

    /**
     * 获取unionid  LgiPFZRlsXceenh4GiieLoaQiEiE
     * @throws Exception
     */
    public  String  getUnionId(String accessToken, String userid) {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid(userid);
            OapiV2UserGetResponse rsp = client.execute(req, accessToken);
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(rsp.getBody());
            JsonNode resultNode = rootNode.path("result");
            String unionId = resultNode.path("unionid").asText();
            return unionId;
        } catch (ApiException e) {
            logger.error("获取 unionId 错误", e);
        } catch (JsonMappingException e) {
            throw new RuntimeException(e);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

        /**
     * 获取钉盘Spaceid  682420026
     * @throws Exception
     */
        public static com.aliyun.dingtalkdrive_1_0.Client createClient() throws Exception {
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
            config.protocol = "https";
            config.regionId = "central";
            return new com.aliyun.dingtalkdrive_1_0.Client(config);
        }

    public static String getSpaceId(String accessToken , String unionId) throws Exception {
        com.aliyun.dingtalkdrive_1_0.Client client = createClient();
        com.aliyun.dingtalkdrive_1_0.models.ListSpacesHeaders listSpacesHeaders = new com.aliyun.dingtalkdrive_1_0.models.ListSpacesHeaders();
        listSpacesHeaders.xAcsDingtalkAccessToken = accessToken;
        com.aliyun.dingtalkdrive_1_0.models.ListSpacesRequest listSpacesRequest = new com.aliyun.dingtalkdrive_1_0.models.ListSpacesRequest()
                .setSpaceType("org")
                .setMaxResults(10)
                .setUnionId(unionId);
        try {
            var response = client.listSpacesWithOptions(listSpacesRequest, listSpacesHeaders, new com.aliyun.teautil.models.RuntimeOptions()).getBody().getSpaces();
            return response.get(1).getSpaceId();
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                logger.error("Error code: {}, Error message: {}", err.code, err.message, err);
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                logger.error("Error code: {}, Error message: {}", err.code, err.message, err);
            }

        }
        return null;
    }

}
